<?php
namespace app\admin\model;

use think\Model;  
use think\Loader;
use think\Validate;
use think\Db;
class Banner  extends Model
{

  protected  $pk='banner_id';//主键
  protected $table = 'xcx_banner';
    public function add_banner($data)
    {
    	//halt($data);
    	//yanzheng
    	$validate = Loader::validate('Banner');

		if(!$validate->check($data)){
		    return ['valid'=>0,'msg'=>$validate->getError()];
		}
	   $arr['banner_name']=$data['banner_name'];
	   $arr['banner_description']=$data['banner_description'];
	   $arr['banner_sort']=$data['banner_sort'];
	   $arr['banner_states']=1;
	   $arr['createtime']=time();
		//判断名称是否唯一
        if(!$data['banner_id'])
        {
            $isNotRepeat=$this->where('banner_name',$data['banner_name'])->find();
            if($isNotRepeat['banner_id']!="")
            {
                return ['valid'=>0,'msg'=>'名称重复'];
                exit;
            }
        }
        if($data['banner_id'])
        {
            $result = $this->save($arr,['banner_id'=>$data['banner_id']]);
        }else
            {
                $result = $this->save($arr);
            }

		if(false === $result){
		    // 验证失败 输出错误信息
		    return ['valid'=>1,'msg'=>'操作失败'];
		}else{
			//添加操作记录
			return ['valid'=>4,'msg'=>'操作成功'];
		}

    }
    public function getBannerTypeDateStates()
    {
    	$res=$this->where('banner_states',1)->order('banner_sort desc')->select();

    	return $res;
    }
    public function getBannerTypeDate()
    {
        $res=$this->order('banner_sort desc')->select();

        return $res;
    }
        public function getBannerTypeInfo($id)
        {
            $res=$this->where('banner_id',$id)->find();
            return $res;
        }
    /**
     * 根据banner_id获得值
     */
    public function getBannerTypeIdData($id)
    {
    	$res=$this->order('banner_sort desc')
    				->where('banner_id',$id)
    	            ->find();
    	return $res;
    }
    /**
     * 查询轮播图信息
     */
    public function getBannerModularDate($id)
    {
    	$res=Db::table('xcx_banner_item')
				->alias('m')
				->join('banner r','m.banner_id = r.banner_id')
				->join('bannerimg g','m.banimg_id = g.banimg_id')
				->where('m.banner_id',$id)
                ->where('m.bitem_states',1)
				->order('bitem_sort')
				//->field('')
				->select();
		return $res;
    }
    public function getOneInfo($id)
    {
        $res=db('banner_item')->where('bitem_id',$id)->find();
        $res['img']=db('bannerimg')->where('banimg_id',$res['banimg_id'])->value('img_url');
        $res['banner']=$this->where('banner_id',$res['banner_id'])->find();
        return $res;
    }
    //下架
    public function shelf($data)
    {
        $pspeci_states=$this->where('banner_id',$data['id'])->value('banner_states');

        if($pspeci_states==1)
        {
            $result=$this->where('banner_id',$data['id'])->update(['banner_states'=>0]);
        }else
        {
            $result=$this->where('banner_id',$data['id'])->update(['banner_states'=>1]);
        }
        if(false === $result){
            // 验证失败 输出错误信息
            return ['valid'=>1,'msg'=>'操作失败'];
        }else{
            return ['valid'=>4,'msg'=>'操作成功'];
        }
    }

}
